import { fileURLToPath, URL } from 'node:url'
//import legacyPlugin from '@vitejs/plugin-legacy'
import legacy from '@vitejs/plugin-legacy';

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
// polyfill.js
import 'core-js/stable';
import 'regenerator-runtime/runtime';
// https://vitejs.dev/config/

export default defineConfig({
  base: './',
  plugins: [vue(), vueJsx(),
    legacy({
      targets: ['defaults', 'ie >= 11', 'chrome >= 49' ,'chrome 66'],
      additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
      renderLegacyChunks:true,
      polyfills:[
        'es.symbol',
        'es.array.filter',
        'es.promise',
        'es.promise.finally',
        'es/map',
        'es/set',
        'es.array.for-each',
        'es.object.define-properties',
        'es.object.define-property',
        'es.object.get-own-property-descriptor',
        'es.object.get-own-property-descriptors',
        'es.object.keys',
        'es.object.to-string',
        'web.dom-collections.for-each',
        'esnext.global-this',
        'esnext.string.match-all'
      ]
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    port: 8892,
    proxy: {
      '/kl': {
        target: 'http://192.168.1.220:7001',
        changeOrigin: true
        // rewrite: (path) => path.replace(/^\/kl/, '')
      }
    },
    hmr: false, // 禁用 HMR
  },
  build: {
    outDir: 'dist',
    target: 'es2015', // or an earlier standard like es2015, es5
    assetsDir: 'assets',
    emptyOutDir: true,
    rollupOptions: {
      input: {
        main: fileURLToPath(new URL('index.html', import.meta.url)),
       // polyfill: 'polyfill.js'
      }
    }
  }
})
